-
Notifications
You must be signed in to change notification settings - Fork 17
Introduce a dedicated PaginatedResource object #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
birdcar
wants to merge
1
commit into
main
Choose a base branch
from
birdcar/better-paginated-resources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Previously, when returning data from a resource that was paginated, we
would parse the pagination args off the resposne, loop over the "data"
value in the response, and map each item in the JSON array to a
specific resource. An example of this would be in the listUsers() method
of the UserManagement class (truncated example below):
```php
$users = [];
list($before, $after) = Util\Request::parsePaginationArgs($response);
foreach ($response["data"] as $responseData) {
\array_push($users, Resource\User::constructFromResponse($responseData));
}
return [$before, $after, $users];
```
Performing this pattern over and over again resulted in a lot of
duplicate code that was doing basically nothing more than an array_map.
Additionally, this return is extremely limited and forces the user into
a limited and specific pattern of either bare array destructuring:
```php
[$before, $after, $users] = $userManagement->listUsers();
```
Or dealing with 0-indexed array values:
```php
$result = $userManagement->listUsers();
```
If for example they just want the first 5 users and don't care about paginating,
this means they'd to either write destructuring that has empty values:
```php
// Huh?
[,,$users] = $userManagement->listUsers(limit: 5);
```
Or they'd have to drop down to
```php
$results = $userManagement->listUsers(limit: 5);
// How do I discover or know what this index is?
$users = $results[2];
```
To fix both of these issues, without affecting current library
consumers, I'm proposing that we create a `Resource\PaginatedResource` class that:
1. DRYs and standardizes the creation of a paginated resource
2. Handles the resource mapping from the data array
3. Continues to allow for bare destructuring (backwards compatible)
4. Introduces named destructuring (e.g `$result["after"]` or
`["users" => $fiveUsers] = $userManagement->listUsers(limit:5)`)
5. Introduces fluent property access (e.g. `$result->after` or
`$result->users`)
The change is fully backwards compatible, cleans up existing resource
code and allows for developers to use the library in whichever code
style is consistent with their project.
For example, it lets you turn this code:
```php
[$before, $after, $users] = $userManagement->listUsers();
while ($after) {
[$before, $after, $currentPage] = $sso->listConnections(
limit: 100,
after: $after,
order: "desc"
);
$users = array_merge($users, $currentPage);
}
```
Into this code:
```php
$users = [];
$after = null;
do {
$result = $userManagement->listUsers(after: $after, limit: 10);
$users = array_merge($allUsers, $result->users);
$after = $result->after;
} while ($after !== null);
```
30b94ee to
d35cb34
Compare
Author
Contributor
Greptile SummaryThis PR introduces Key Changes:
Benefits:
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client as Client Code
participant API as API Class<br/>(UserManagement/SSO/etc)
participant HTTPClient as HTTP Client
participant PaginatedResource as PaginatedResource
participant ResourceClass as Resource Class<br/>(User/Directory/etc)
Client->>API: listUsers(params)
API->>HTTPClient: request(GET, path, params)
HTTPClient-->>API: response JSON
API->>PaginatedResource: constructFromResponse(response, Resource\User::class, 'users')
PaginatedResource->>PaginatedResource: parsePaginationArgs(response)
Note over PaginatedResource: Extracts before/after cursors<br/>from list_metadata
loop For each item in response["data"]
PaginatedResource->>ResourceClass: User::constructFromResponse(item)
ResourceClass-->>PaginatedResource: User instance
end
PaginatedResource->>PaginatedResource: new PaginatedResource(before, after, data, 'users')
PaginatedResource-->>API: PaginatedResource instance
API-->>Client: PaginatedResource
Note over Client: Multiple access patterns available:
Client->>PaginatedResource: Bare destructuring: [$before, $after, $users]
Client->>PaginatedResource: Named access: ["users" => $users]
Client->>PaginatedResource: Fluent access: $result->users
|
Contributor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
11 files reviewed, no comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Previously, when returning data from a resource that was paginated, we would parse the pagination args off the response, loop over the "data" value in the response, and map each item in the JSON array to a specific resource. An example of this would be in the
listUsers()method of theUserManagementclass (truncated example below):Performing this pattern over and over again resulted in a lot of duplicate code that was doing basically nothing more than an array_map.
Additionally, this return is extremely limited and forces the user into a limited and specific pattern of either bare array destructuring:
Or dealing with 0-indexed array values:
If for example they just want the first 5 users and don't care about paginating, this means they'd need to either write a destructuring expression that has empty values:
Or they'd have to drop down to:
To fix both of these issues, without affecting current library consumers, I'm proposing that we create a
Resource\PaginatedResourceclass that:Resource\PaginatedResource::constructFromResponse($response, Resource\User::class, 'users');)$result["after"]or["users" => $fiveUsers] = $userManagement->listUsers(limit:5))$result->afteror$result->users)The change is fully backwards compatible, cleans up existing resource code and allows for developers to use the library in whichever code style is consistent with their project.
For example, it lets you turn this code:
Into this code:
Documentation
Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.
If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.